import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
import groovyHint from "./groovy";
import javascriptHint from "./javascript";

const languageHints = {
  java: groovyHint,
  groovy: groovyHint,
  js: javascriptHint,
  javascript: javascriptHint,
}; 

// 已注册代码提示的语言
const languageRegistered = [];

class MonacoEditorHint {
  /**
   * @param  {...any} args arg[0]为语言 与monaco支持的语言保持一致
   * @returns
   */
  static init(...args) {
    args[1] && console.log("args[1] =", args[1]);

    const language = args[0];
    if (!language) return;

    const targetHint = languageHints[language];
    if (!targetHint) return;

    if (languageRegistered.includes(language)) return;
    languageRegistered.push(language);

    monaco.languages.registerCompletionItemProvider(language, {
      provideCompletionItems: (model, position) => {
        const line = position.lineNumber;
        const column = position.column;
        const content = model.getLineContent(line);
        const sym = content[column - 2];
        var reg = `^` + sym;
        const suggestions = targetHint
          .filter((item) => item.label.match(new RegExp(reg)))
          .map((item) => ({
            // kind,
            ...item,
            insertTextRules:
              monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          }));
        return {
          suggestions,
        };
      },
    });
  }
}


export default MonacoEditorHint